package org.zj.webview.app.lifecycle;

import lombok.AllArgsConstructor;
import org.zj.webview.app.config.WebViewConfig;
import org.noear.solon.annotation.Component;
import org.noear.solon.core.event.AppLoadEndEvent;
import org.noear.solon.core.event.EventBus;
import org.noear.solon.core.event.EventListener;
import org.zj.webview.WebViewApi;

/**
 * @author zj
 * @since 2025/7/25
 */
@AllArgsConstructor
@Component
public class WebViewLoader implements EventListener<AppLoadEndEvent> {

    private final WebViewConfig config;

    @Override
    public void onEvent(AppLoadEndEvent event) {
        WebViewApi.setWebView2EnvironmentSettings(config.getSettings());
        WebViewApi.addStateEventListener(EventBus::publish);

        Thread thread = new Thread(WebViewApi::runWebView2Loop, "webview2-loop");
        thread.start();
    }
}